import { _decorator, Camera, Component, EventKeyboard, Input, input, KeyCode, Node, Quat, Tween, tween, v3, Vec3 } from 'cc';
import { envObject } from './envObject';
const { ccclass, property } = _decorator;

export enum EMoveDir {
    keep = -1,
    left = 0,
    right = 1,
    up = 2,
    down = 3
}

@ccclass('defaultMapCtl')
export class defaultMapCtl extends Component {
    dir: number;

    @property(Camera) mainCamera: Camera = null
    @property(Node) role: Node = null
    start() {
        input.on(Input.EventType.KEY_DOWN, this.onClickKD, this)
    }

    _moveDir: EMoveDir = EMoveDir.keep
    onClickKD(event: EventKeyboard) {
        console.log(event)
        let dir = 0
        if (event.keyCode == KeyCode.KEY_Q) {
            dir = -1
        } else if (event.keyCode == KeyCode.KEY_E) {
            dir = 1
        } else if (event.keyCode == KeyCode.KEY_A) {
            this._moveDir = EMoveDir.left
        } else if (event.keyCode == KeyCode.KEY_S) {
            this._moveDir = EMoveDir.down
        } else if (event.keyCode == KeyCode.KEY_W) {
            this._moveDir = EMoveDir.up
        } else if (event.keyCode == KeyCode.KEY_D) {
            this._moveDir = EMoveDir.right
        } else {
            this._moveDir = EMoveDir.keep
        }
        this.dir = dir

        if (this._isRotate) {
            return
        }


        this._isRotate = true
        let curRotation: Quat = this.mainCamera.node.parent.rotation
        let curEulerAngles = this.mainCamera.node.parent.eulerAngles
        curRotation.getEulerAngles(this._tmpRotate)

        console.log("当前四元数转换旋转角度...", this._tmpRotate)

        Tween.stopAllByTarget(this.mainCamera.node.parent)
        tween(this.mainCamera.node.parent)
            .to(1, { eulerAngles: v3(curEulerAngles.x, curEulerAngles.y, curEulerAngles.z + this.dir * 30) })
            .call(() => {
                this._isRotate = false
                this.dir = 0
                // let tmpQuat = this.mainCamera.node.rotation
                // tmpQuat.getEulerAngles(this._tmpRotate)
                // console.log("修改摄像机角度...", this.mainCamera.node.rotation, this._tmpRotate, this.mainCamera.node.eulerAngles)
            }).start()
    }

    _changeRotate: number = 30
    _isRotate: boolean = false
    _hasChangeRotate = 0
    _tmpRotate: Vec3 = v3()

    _mapObj: Array<Node> = []
    _tmpCamerePos: Vec3 = v3()
    protected update(dt: number): void {
        this._tmpCamerePos = this.mainCamera.node.worldPosition

        if (this._mapObj.length <= 0) {
            let tObj = this.node.children
            for (let i = 0; i < tObj.length; i++) {
                let tNode = tObj[i]
                if (tNode.getComponent(envObject)) {
                    this._mapObj.push(tNode)
                }
            }
        } else {
            this._mapObj.sort((obj1: Node, obj2: Node) => {
                let dis1 = obj1.worldPosition.clone().subtract(this._tmpCamerePos).lengthSqr()
                let dis2 = obj2.worldPosition.clone().subtract(this._tmpCamerePos).lengthSqr()
                return dis2 - dis1
            })
            for (let i = 0; i < this._mapObj.length; i++) {
                let tNode = this._mapObj[i]
                tNode.setSiblingIndex(i)
            }
        }


        let tRolePos = this.role.position.clone()
        let mPos = null
        let speed = 2
        if (this._moveDir != EMoveDir.keep) {
            switch (this._moveDir) {
                case EMoveDir.down:
                    mPos = (this.role.up.clone().multiplyScalar(-1))
                    break;
                case EMoveDir.up:
                    mPos = (this.role.up.clone())
                    break;
                case EMoveDir.left:
                    mPos = (this.role.right.clone().multiplyScalar(-1))
                    break;
                case EMoveDir.right:
                    mPos = (this.role.right.clone())
                default:
                    break;
            }
            if (mPos) {
                mPos.multiplyScalar(speed)
                mPos.z = 0
                tRolePos.add(mPos)
                this.role.setPosition(tRolePos)
            }
        }
    }

}


